package com.heraleign.test.kafka;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;


    public void send(String message){
        String topic = "kafka-test";
        // 在这里消息的发送有kafka有很多种发送方法，可以进行参考，选择合适的发送方式
        ProducerRecord<String,String> producerRecord = new ProducerRecord<String, String>(topic, message);
        kafkaTemplate.send(producerRecord);
        System.out.println("Kafka发送消息:" + producerRecord.value());
    }
}
